/*!
 * avangard/battle_controller.js
 * © 2009 avangardonline.ru
 * @author BlooDHounD <http://www.controller.by>
 */

if ( !window.controller ) throw new Error( '"controller" not initialized' );

controller.require( 'avangard' );

if ( !avangard.BattleController ) {

	controller.require( 'controller.events.EventDispatcher' );
	controller.require( 'controller.utils' );

	/**
	 * класс работы с чатом
	 * @class
	 * @namespace	avangard
	 * @extends		controller.events.EventDispatcher
	 * @author		BlooDHounD	<http://www.controller.by>
	 */
	avangard.BattleController = ( function() {

		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 */
		var	win =		window,

			b =			controller,
			utils =		b.utils;

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		/**
		 * @constructor
		 * @param	{controller.Flash.ExternalFlash}	connection
		 * @param	{Number}						type
		 */
		var BattleController = function(connection, type, redirectURI) {
			BattleController.superPrototype.constructor.call( this );
			this._connection = connection;
			this._type = type;
			this.redirectURI = redirectURI;
			// инитиализируем
			if ( connection.isInitialized() ) {
				initHandler.call( this );
			} else {
				connection.addEventListener( 'init', this, initHandler );
			}
		};

		b.extend( BattleController, b.events.EventDispatcher );

		var BattleControllerPrototype = BattleController.prototype;

		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * @type	{Number}
		 */
		BattleControllerPrototype._type = 0;

		/**
		 * @private
		 * @type	{controller.Flash.ExternalFlash}
		 */
		BattleControllerPrototype._connection = null;

		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * @type	{Number}
		 */
		BattleControllerPrototype.redirectURI = null;

		//--------------------------------------------------------------------------
		//
		//  Event handlers
		//
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * флэшка проинитиализировалась
		 */
		var initHandler = function() {
			// удаляем слушателя инитиализации
			this._connection.removeEventListener( 'init', this, initHandler );
			// вешаем хэнлеры на методы
			if ( this._type != 1 ) {
				this._connection.closeBattle =	utils.createDeferredDelegate( this, this.closeBattle );
			}
		};

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		/**
		 */
		BattleControllerPrototype.closeBattle = function() {
			if ( this.redirectURI ) {
				win.location = this.redirectURI;
			} else {
				this._connection.dispose();
			}
		};

		/**
		 * @return	{String}
		 */
		BattleControllerPrototype.toString = function() {
			return '[BattleController object]';
		};

		//--------------------------------------------------------------------------
		//
		//  Class methods
		//
		//--------------------------------------------------------------------------

		/**
		 * @static
		 * @method
		 * @return	{String}
		 */
		BattleController.toString = function() {
			return '[class BattleController]';
		};

		return BattleController;

	}() );

}